package com.ambercrm.tools;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;

import com.amap.api.maps.model.LatLng;

public class MapUtils {

    // 计算地图两点之间的距离
    public static String calculateLineDistance(LatLng start, LatLng end) {
        if ((start == null) || (end == null)) {
            return "0公里";
        }
        double d1 = 0.01745329251994329D;
        double d2 = start.longitude;
        double d3 = start.latitude;
        double d4 = end.longitude;
        double d5 = end.latitude;
        d2 *= d1;
        d3 *= d1;
        d4 *= d1;
        d5 *= d1;
        double d6 = Math.sin(d2);
        double d7 = Math.sin(d3);
        double d8 = Math.cos(d2);
        double d9 = Math.cos(d3);
        double d10 = Math.sin(d4);
        double d11 = Math.sin(d5);
        double d12 = Math.cos(d4);
        double d13 = Math.cos(d5);
        double[] arrayOfDouble1 = new double[3];
        double[] arrayOfDouble2 = new double[3];
        arrayOfDouble1[0] = (d9 * d8);
        arrayOfDouble1[1] = (d9 * d6);
        arrayOfDouble1[2] = d7;
        arrayOfDouble2[0] = (d13 * d12);
        arrayOfDouble2[1] = (d13 * d10);
        arrayOfDouble2[2] = d11;
        double d14 = Math.sqrt((arrayOfDouble1[0] - arrayOfDouble2[0]) * (arrayOfDouble1[0] - arrayOfDouble2[0])
                + (arrayOfDouble1[1] - arrayOfDouble2[1]) * (arrayOfDouble1[1] - arrayOfDouble2[1])
                + (arrayOfDouble1[2] - arrayOfDouble2[2]) * (arrayOfDouble1[2] - arrayOfDouble2[2]));

        double distance = (Math.asin(d14 / 2.0D) * 12742001.579854401D);
        if (distance < 1000) {
            return String.format("%.1f", distance) + "米";
        }

        return String.format("%.1f", distance / 1000) + "公里";
    }

    // 参数：要去的地方的名字、经纬度， 官网：http://lbsyun.baidu.com/index.php?title=uri/api/android
    public static void goToBaiduMap(Context context, String dName, double latitude, double longitude) {
        String desName = null;
        if (dName == null) {
            desName = "终点";
        }
        else {
            desName = dName;
        }
        String uri = "baidumap://map/direction"
                + "?origin=我的位置"
                +"&destination=name:" + desName + "|latlng:"+latitude+","+longitude
                +"&coord_type=bd09ll"
                + "&mode=driving"
                + "&src=andr.companyName.appName";//src为统计来源必填，companyName、appName是公司名和应用名
        Intent intent = new Intent("android.intent.action.VIEW", Uri.parse(uri));
        intent.addCategory("android.intent.category.DEFAULT");
        context.startActivity(intent);
    }

    // 官网：https://lbs.amap.com/api/uri-api/guide/travel/route
    public static void goToGaodeMap(Context context, String dName, double latitude, double longitude) {
        String desName = null;
        if (dName == null) {
            desName = "终点";
        }
        else {
            desName = dName;
        }
        //默认驾车
        String uri = "amapuri://route/plan/"
                + "?dlat="+ latitude+"&dlon="+longitude
                + "&sname=我的位置"
                + "&dname=" + desName
                + "&dev=1"
                + "&t=0";
        Intent intent = new Intent("android.intent.action.VIEW", Uri.parse(uri));
        intent.addCategory("android.intent.category.DEFAULT");
        intent.setPackage("com.autonavi.minimap");
        context.startActivity(intent);
    }

    // 腾讯地图，参数参考：https://lbs.qq.com/uri_v1/guide-mobile-navAndRoute.html
    // 参数：起始点坐标，起始点名字，重点坐标，终点名字，自己app名字
    public static void goTencentMap(Context context, String startPoint, String startAddressName, String endPoint, String endAddressName, String appName){
        if (startPoint != null && startAddressName != null) {
            Uri uri = Uri.parse("qqmap://map/routeplan?type=drive" + "&from=" + startAddressName + "&fromcoord=" + startPoint + "&to=" + endAddressName + "&tocoord=" + endPoint + "&referer=" + appName);
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            context.startActivity(intent);
        }
        else {
            Uri uri = Uri.parse("qqmap://map/routeplan?type=drive" + "&to=" + endAddressName + "&tocoord=" + endPoint + "&referer=" + appName);
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            context.startActivity(intent);
        }
    }
}
